package com.fanfanwang.ffw.app.adapter;

import java.util.ArrayList;

import com.fanfanwang.ffw.app.asynctask.SetParknumStatusAsyncTask;
import com.fanfanwang.ffw.app.bean.CarportBean;
import com.fanfanwang.ffw.app.bean.ParkNumBean;
import com.fanfanwang.ffw.app.bean.SectionBean;
import com.fanfanwang.ffw.app.bean.ServiceBean;
import com.fanfanwang.ffw.app.tenant.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;


public class GlyCarportStatusAdapter extends BaseAdapter {

	private ArrayList<ParkNumBean> beans;
	private Activity mContext;
	private String status="0";
	public GlyCarportStatusAdapter(Activity mContext,ArrayList<ParkNumBean> beans) {
		// TODO Auto-generated constructor stub
		this.mContext = mContext;
		this.beans = beans;
	}
	public void resetData(ArrayList<ParkNumBean> beans){
		this.beans=beans;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beans.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_for_carport_status, null);
			holder = new ViewHolder();
			holder.carport_num = (TextView) convertView.findViewById(R.id.carport_num);
			holder.tip=(TextView) convertView.findViewById(R.id.carport_tip);
			holder.button=(ToggleButton) convertView.findViewById(R.id.switch_btn);

			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.carport_num.setText(beans.get(position).parknum);
//		holder.carport_num.setText("001");
		if(("0").equals(beans.get(position).status)){//0表示空闲,1有车
			holder.button.setChecked(false);
		}else{
			holder.button.setChecked(true);
		}

		holder.button.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub

				if(isChecked)
				{status="1";
				}else{
					status="0";
				}

			}
		});
		holder.button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(!(beans.get(position).status).equals(status)){
					if(("0").equals(status))
					{
						holder.button.setChecked(false);
					}else{
						holder.button.setChecked(true);
					}
					//记得改一下adapter里面这个值的数据，否则一滑动会重新调用这个方法得到这个bean的数据还是以前的。
					beans.get(position).status=status;
					new SetParknumStatusAsyncTask(mContext, beans.get(position).id, status).execute("");
				}
			}
		});

		return convertView;
	}

	class ViewHolder {
		TextView carport_num;
		TextView tip;//提示按钮的意思
		ToggleButton button;
	}


}
